package com.smoed.common.utilities.language;

import com.aliasi.classify.JointClassifier;
import com.aliasi.util.AbstractExternalizable;

import java.io.File;
import java.io.IOException;

public final class LanguageIdentifier {
    private static final File languageModelFile = new File("models/languageIdClassifier.model");
    private static JointClassifier<CharSequence> languageClassifier;

    static {
        try {
            languageClassifier = (JointClassifier<CharSequence>) AbstractExternalizable.readObject(languageModelFile);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static synchronized LanguageId getLanguageId(String text) {
        return LanguageId.valueOf(languageClassifier.classify(text).bestCategory());
    }
}
